//
//  ExchangeDiscountTableViewCell.m
//  Findif
//
//  Created by Mac on 2019/4/10.
//  Copyright © 2019年 Cicel Chen. All rights reserved.
//

#import "ExchangeDiscountTableViewCell.h"

@interface ExchangeDiscountTableViewCell ()

@property (nonatomic, weak)UIView * backView;
@property (nonatomic, weak)UIImageView * discountImageView;
@property (nonatomic, weak)UILabel * discountLabel;
@property (nonatomic, weak)UILabel * titleLabel;
@property (nonatomic, weak)UILabel * fenLabel;
@property (nonatomic, weak)UILabel * exploreLabel;
@property (nonatomic, weak)UIButton * exchangeButton;
@property (nonatomic, weak)UIView * lineView;

@end

@implementation ExchangeDiscountTableViewCell


-(void)setModel:(MoneyModel *)model{
    
    _model = model;
    
    NSString * cateStr = model.merchants_coupons_info[@"usable_goods_category_name"];
    self.titleLabel.text = [NSString stringWithFormat:@"%@·%@",cateStr,model.name];
    self.fenLabel.text = [NSString stringWithFormat:@"%@积分",model.value];
    self.ID = [NSString stringWithFormat:@"%@",model.id];
    int type = [model.merchants_coupons_info[@"type"] intValue];
    self.exploreLabel.text = [NSString stringWithFormat:@"满%@元可用",model.merchants_coupons_info[@"use_standard"]];
    if (type == 1) {
        
        self.discountLabel.text = [NSString stringWithFormat:@"%@元",model.merchants_coupons_info[@"discount"]];
    }else if (type == 2){
        
        self.discountLabel.text = [NSString stringWithFormat:@"%@折",model.merchants_coupons_info[@"discount"]];
    }
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    
    if ([super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        [self setupUI];
    }
    
    return self;
}


- (void)setupUI{
    
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    self.backgroundColor = ColorWithRGB(0xEFF0F1);
    
    UIView * backView = [UIView new];
    
    backView.backgroundColor = ZRWhiteColor;
    backView.userInteractionEnabled = YES;
    backView.layer.cornerRadius = 5 * SCREENH6;
    backView.layer.masksToBounds = YES;
    
    self.backView = backView;
    
    [self.contentView addSubview:self.backView];
    
    UIImageView * discountImageView = [UIImageView new];
    
    discountImageView.image = [UIImage imageNamed:@"兑换优惠券"];
    
    self.discountImageView = discountImageView;
    
    [self.backView addSubview:self.discountImageView];
    
    UILabel * discountLabel = [UILabel new];
    //优惠券
    discountLabel.textColor = ColorWithRGB(0xF0614C);
    discountLabel.font = [UIFont systemFontOfSize:14 * SCREENH6];
    discountLabel.textAlignment = NSTextAlignmentCenter;
    
    self.discountLabel = discountLabel;
    
    [self.discountImageView addSubview:self.discountLabel];
    
    
    UILabel * titleLabel = [UILabel new];
    
    titleLabel.textColor = ZRBlackColor;
    titleLabel.text = @"----";
    titleLabel.font = [UIFont systemFontOfSize:15 * SCREENH6];
    
    self.titleLabel = titleLabel;
    
    [self.backView addSubview:self.titleLabel];
    
    
    UILabel * exploreLabel = [UILabel new];
    
    exploreLabel.textColor = [UIColor lightGrayColor];
    exploreLabel.text = @"----";
    exploreLabel.font = [UIFont systemFontOfSize:12 * SCREENH6];
    
    self.exploreLabel = exploreLabel;
    
    [self.backView addSubview:self.exploreLabel];
    
    
    UILabel * fenLabel = [UILabel new];
    
    fenLabel.textColor = ColorWithRGB(0xFFD426);
    fenLabel.text = @"----";
    fenLabel.font = [UIFont boldSystemFontOfSize:13* SCREENH6];
    
    self.fenLabel = fenLabel;
    
    [self.backView addSubview:self.fenLabel];
    
    
    UIButton * exchangeButton = [UIButton new];
    
    [exchangeButton setTitle:@"兑换" forState:UIControlStateNormal];
    [exchangeButton setTitleColor:ColorWithRGB(0xFFD426) forState:UIControlStateNormal];
    exchangeButton.titleLabel.font = [UIFont systemFontOfSize:15 * SCREENH6];
    exchangeButton.layer.borderColor = [ColorWithRGB(0xFFD426) CGColor];
    exchangeButton.layer.borderWidth = 1.0f;
    exchangeButton.layer.cornerRadius = 5 * SCREENH6;
    exchangeButton.layer.masksToBounds = YES;
    [exchangeButton addTarget:self action:@selector(clickExchangeButton) forControlEvents:UIControlEventTouchUpInside];
    
    self.exchangeButton = exchangeButton;
    
    [self.backView addSubview:self.exchangeButton];
    
    
    UIView * lineView = [UIView new];
    
    lineView.backgroundColor = ColorWithRGB(0xe4e4e4);
    
    self.lineView = lineView;
    
    [self.backView addSubview:self.lineView];
}


-(void)layoutSubviews{
    
    [self.backView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.contentView).offset(15 * SCREENH6);
        make.trailing.mas_equalTo(self.contentView).offset(-15 * SCREENH6);
        make.height.mas_equalTo(90 * SCREENH6);
        make.centerY.mas_equalTo(self.contentView);
    }];
    
    [self.discountImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.backView).offset(10 * SCREENH6);
        make.centerY.mas_equalTo(self.backView);
        make.width.mas_equalTo(55 * SCREENH6);
        make.height.mas_equalTo(65 * SCREENH6);
    }];
    
    [self.discountLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.centerX.mas_equalTo(self.discountImageView);
    }];
    
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.discountImageView).offset(5 * SCREENH6);
        make.leading.mas_equalTo(self.discountImageView.mas_trailing).offset(10 * SCREENH6);
    }];
    
    [self.exploreLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.titleLabel.mas_bottom).offset(5 * SCREENH6);
        make.leading.mas_equalTo(self.titleLabel);
    }];
    
    [self.fenLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.exploreLabel);
        make.top.mas_equalTo(self.exploreLabel.mas_bottom).offset(5 * SCREENH6);
    }];
    
    [self.exchangeButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.mas_equalTo(self.backView).offset(-10 * SCREENH6);
        make.centerY.mas_equalTo(self.backView);
        make.width.mas_equalTo(60 * SCREENH6);
        make.height.mas_equalTo(30 * SCREENH6);
    }];
    
    [self.lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.backView).offset(10 * SCREENH6);
        make.trailing.mas_equalTo(self.backView).offset(-10 * SCREENH6);
        make.bottom.mas_equalTo(self.backView);
        make.height.mas_equalTo(1 * SCREENH6);
    }];
}


- (void)clickExchangeButton{
    
    NSString * ID = [NSString stringWithFormat:@"%@",self.model.id];
    
    if (self.delegate && [self.delegate respondsToSelector:@selector(clickExchangeDiscountButtonID:)])
    {
        [self.delegate clickExchangeDiscountButtonID:ID];
    }
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
